//
//  DeviceModel.m
//  Siterwell Link
//
//  Created by tracyhenry on 2022/6/14.
//  Copyright © 2022 Siterwell. All rights reserved.
//

#import "DeviceModel.h"
#import "NSString+CY.h"
#import "SiterHelp.h"
#import <objc/message.h>
#import "TimeHelper.h"

@interface DeviceModel()

@property(nonatomic,strong)NSMutableArray <Ignore>*shadowList;

@end

@implementation DeviceModel

-(instancetype)initWithDic:(NSDictionary *)dic withThingName:(NSString *)thingName{
    if(self=[super init]){
        self.thingName = thingName;
        unsigned int count;
        objc_property_t *propertyList = class_copyPropertyList([self class], &count);
        
        NSDictionary *de = dic[@"state"][@"reported"][@"DevInfo"];
        NSDictionary *de2 = dic[@"state"][@"reported"][@"DevConfig"];
        NSDictionary *de3 = dic[@"state"][@"reported"][@"Factory"];
        
        NSDictionary *de_timestamp = dic[@"metadata"][@"reported"][@"DevInfo"];
        NSDictionary *de2_timestamp = dic[@"metadata"][@"reported"][@"DevConfig"];
        NSDictionary *de3_timestamp = dic[@"metadata"][@"reported"][@"Factory"];
        
        NSString *mac = dic[@"state"][@"reported"][@"mac"];
        NSString *SerialNumber = dic[@"state"][@"reported"][@"SerialNumber"];
        NSString *ssid = dic[@"state"][@"reported"][@"ssid"];
        NSString *hwrev = dic[@"state"][@"reported"][@"hwrev"];
        
        [self setMac:mac];
        [self setSn:SerialNumber];
        [self setSsid:ssid];
        [self setFirmwareVersion:hwrev];
        
        self.shadowList = [[NSMutableArray alloc] init];
        for (int i = 0; i < count; i++) {
            objc_property_t property = propertyList[i];
            const char *cName = property_getName(property);
            NSString *name = [NSString stringWithUTF8String:cName];
            if(de!=nil){
                NSObject *obj = de[name];
                if(obj!=nil){
                    [self setValue:obj forKey:name];
                    if(de_timestamp[name][@"timestamp"]!=nil){
                        [self.shadowList addObject:@{@"thingName":thingName,@"shadow":name,@"value":obj,@"timestamp":de_timestamp[name][@"timestamp"]}];
                    }else{
                        NSDate *date = [NSDate new];
                        NSInteger time = (long)[date timeIntervalSince1970];
                        [self.shadowList addObject:@{@"thingName":thingName,@"shadow":name,@"value":obj,@"timestamp":@(time)}];
                    }
                }
            }

            if(de2!=nil){
                NSObject *obj2 = de2[name];
                if(obj2!=nil){
                    [self setValue:obj2 forKey:name];
                    if(de2_timestamp[name][@"timestamp"]!=nil){
                        [self.shadowList addObject:@{@"thingName":thingName,@"shadow":name,@"value":obj2,@"timestamp":de2_timestamp[name][@"timestamp"]}];
                    }else{
                        NSDate *date = [NSDate new];
                        NSInteger time = (long)[date timeIntervalSince1970];
                        [self.shadowList addObject:@{@"thingName":thingName,@"shadow":name,@"value":obj2,@"timestamp":@(time)}];
                    }
                }
            }
            
            if(de3!=nil){
                NSObject *obj3 = de3[name];
                if(obj3!=nil){
                    [self setValue:obj3 forKey:name];
                    if(de3_timestamp[name][@"timestamp"]!=nil){
                        [self.shadowList addObject:@{@"thingName":thingName,@"shadow":name,@"value":obj3,@"timestamp":de3_timestamp[name][@"timestamp"]}];
                    }else{
                        NSDate *date = [NSDate new];
                        NSInteger time = (long)[date timeIntervalSince1970];
                        [self.shadowList addObject:@{@"thingName":thingName,@"shadow":name,@"value":obj3,@"timestamp":@(time)}];
                    }
                }
            }
            
        }
    }
    return self;
}

-(instancetype)initWithDicSub:(NSDictionary *)dic withThingName:(NSString *)thingName{
    if(self=[super init]){
        self.thingName = thingName;
        unsigned int count;
        objc_property_t *propertyList = class_copyPropertyList([self class], &count);
        
        
        NSString *fwrev = dic[@"fwrev"];
        NSString *hwrev = dic[@"hwrev"];

        [self setSoftwareVersion:fwrev];
        [self setFirmwareVersion:hwrev];
        
        self.shadowList = [[NSMutableArray alloc] init];
        for (int i = 0; i < count; i++) {
            objc_property_t property = propertyList[i];
            const char *cName = property_getName(property);
            NSString *name = [NSString stringWithUTF8String:cName];
            if(dic!=nil){
                NSObject *obj = dic[name];
                if(obj!=nil){
                    [self setValue:obj forKey:name];
                    NSDate *date = [NSDate new];
                    NSInteger time = (long)[date timeIntervalSince1970];
                    [self.shadowList addObject:@{@"thingName":thingName,@"shadow":name,@"value":obj,@"timestamp":@(time)}];
                }
            }
            
        }
    }
    return self;
}


-(NSString *)getLenStr:(NSString *)content{
    if([NSString isBlankString:content]){
        return @"";
    }else if(content.length%2!=0){
        return @"";
    }else{
        long len = content.length/2;
        return [SiterHelp gethexBybinary:len withContentLength:2];
    }
}


-(NSString *)getContent{
    return @"";
}

-(long)getAlarmLevel{
    return 1;
}

-(BOOL)isOnline{
    return [self.state intValue]==1;
}

-(BOOL)isLowBattery{
    return [self.state intValue]==1;
}


-(void)setAnylisStatus{
    
}

-(void)setBackAnylisStatusStr1:(NSString *)str1 withStr2:(NSString *)str2{
    
}




- (NSDictionary *)dicFromObject {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    unsigned int count;
    objc_property_t *propertyList = class_copyPropertyList([self class], &count);
    
    for (int i = 0; i < count; i++) {
        objc_property_t property = propertyList[i];
        const char *cName = property_getName(property);
        NSString *name = [NSString stringWithUTF8String:cName];
        NSObject *value = [self valueForKey:name];//valueForKey返回的数字和字符串都是对象
        
        if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]) {
            //string , bool, int ,NSinteger
            [dic setObject:value forKey:name];
            
        } else if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSDictionary class]]) {
            //字典或字典
            [dic setObject:[self arrayOrDicWithObject:(NSArray*)value] forKey:name];
            
        } else if (value == nil) {
            //null
            [dic setObject:[NSNull null] forKey:name];//这行可以注释掉?????
            
        } else {
            //model
            [dic setObject:[self dicFromObject] forKey:name];
        }
    }
    
    return [dic copy];
}

//将可能存在model数组转化为普通数组
- (id)arrayOrDicWithObject:(id)origin {
    if ([origin isKindOfClass:[NSArray class]]) {
        //数组
        NSMutableArray *array = [NSMutableArray array];
        for (NSObject *object in origin) {
            if ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSNumber class]]) {
                //string , bool, int ,NSinteger
                [array addObject:object];
                
            } else if ([object isKindOfClass:[NSArray class]] || [object isKindOfClass:[NSDictionary class]]) {
                //数组或字典
                [array addObject:[self arrayOrDicWithObject:(NSArray *)object]];
                
            } else {
                //model
                [array addObject:[self dicFromObject]];
            }
        }
        
        return [array copy];
        
    } else if ([origin isKindOfClass:[NSDictionary class]]) {
        //字典
        NSDictionary *originDic = (NSDictionary *)origin;
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        for (NSString *key in originDic.allKeys) {
            id object = [originDic objectForKey:key];
            
            if ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSNumber class]]) {
                //string , bool, int ,NSinteger
                [dic setObject:object forKey:key];
                
            } else if ([object isKindOfClass:[NSArray class]] || [object isKindOfClass:[NSDictionary class]]) {
                //数组或字典
                [dic setObject:[self arrayOrDicWithObject:object] forKey:key];
                
            } else {
                //model
                [dic setObject:[self dicFromObject] forKey:key];
            }
        }
        
        return [dic copy];
    }
    
    return [NSNull null];
}


-(nonnull NSMutableArray *)getShadowList{
    if(_shadowList==nil){
        _shadowList = [[NSMutableArray alloc] init];
    }
    
    return _shadowList;
}

@end
